package com.qin.malldiscount.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qin.malldiscount.domain.SmsHomeAdvertise;
import com.qin.malldiscount.service.SmsHomeAdvertiseService;
import com.qin.malldiscount.mapper.SmsHomeAdvertiseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author 覃剑
* @description 针对表【sms_home_advertise(首页轮播广告表)】的数据库操作Service实现
* @createDate 2022-11-24 10:42:56
*/
@Service
public class SmsHomeAdvertiseServiceImpl extends ServiceImpl<SmsHomeAdvertiseMapper, SmsHomeAdvertise>
    implements SmsHomeAdvertiseService{

    @Autowired
    private SmsHomeAdvertiseMapper advertiseMapper;

    /**
     * 添加广告
     */
    @Override
    public int create(SmsHomeAdvertise advertise) {
        //初始化点击数
        advertise.setClickCount(0);
        advertise.setOrderCount(0);
        int count = advertiseMapper.insert(advertise);
        return count;
    }

    /**
     * 批量删除广告
     */
    @Override
    public int delete(List<Long> ids) {
        int count = advertiseMapper.deleteBatchIds(ids);
        return count;
    }

    /**
     * 修改上、下线状态
     */
    @Override
    public int updateStatus(Long id, Integer status) {
        SmsHomeAdvertise advertise = new SmsHomeAdvertise();
        advertise.setId(id);
        advertise.setStatus(status);
        int count = advertiseMapper.updateById(advertise);
        return count;
    }

    /**
     * 获取广告详情
     */
    @Override
    public SmsHomeAdvertise getItem(Long id) {
        SmsHomeAdvertise advertise = advertiseMapper.selectById(id);
        return advertise;
    }


    /**
     * 更新广告
     */
    @Override
    public int update(Long id, SmsHomeAdvertise advertise) {
        advertise.setId(id);
        int count = advertiseMapper.updateById(advertise);
        return count;
    }

    /**
     * 分页查询广告
     */
    @Override
    public Page<SmsHomeAdvertise> list(String name, Integer type, String endTime, Integer pageSize, Integer pageNum) {
        Page<SmsHomeAdvertise> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SmsHomeAdvertise> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(name)){
            wrapper.like("name","%"+name+"%");
        }
        if (type!=null){
            wrapper.eq("type",type);
        }
        if (StrUtil.isNotEmpty(endTime)){
            String startStr = endTime + " 00:00:00";
            String endStr = endTime + " 23:59:59";
            //日期转换
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date start = null;
            try {
                start = sdf.parse(startStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date end = null;
            try {
                end = sdf.parse(endStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (start != null && end != null) {
                wrapper.between("end_time",start,end);
            }
            
        }
        //降序
        wrapper.orderByDesc("sort");
        Page<SmsHomeAdvertise> selectPage = advertiseMapper.selectPage(page, wrapper);
        return selectPage;
    }
}




